//
//  ViewController.m
//  4.26
//
//  Created by 王成龙 on 16/4/26.
//  Copyright © 2016年 王成龙. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"

@interface ViewController (){

   // NSArray*data;
    NSMutableArray*aabb;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
 /*   data=@[
           @"123",
           @"456",
           @"789"
           ];
    self.view.backgroundColor=[UIColor purpleColor];
    */
    self.title=@"谢鹏涛电话簿";
    UITableView*tableview=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableview.dataSource=self;
    [self.view addSubview:tableview];
    
   /* NSBundle*bundle=[NSBundle mainBundle];
    NSURL*url=[bundle URLForResource:@"ttb" withExtension:@""];
    NSString*text=[[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"读取数据  %@",text);
    NSArray*arr=[text componentsSeparatedByString:@"\n"];
    NSMutableArray*tmp=[NSMutableArray new];
    for(int i=0;i<arr.count;i++){
        NSString*item=arr[i];
        NSArray*p=[item componentsSeparatedByString:@","];
        Person*per=[Person new];
        per.bianhao=p[0];
        per.name=p[1];
        [tmp addObject:per];
        }
    data=tmp;*/
    NSBundle*bundle=[NSBundle mainBundle];
    NSURL*url=[bundle URLForResource:@"PIM00001" withExtension:@"vcf"];
    NSString *text = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
// NSLog(@"读取数据：%@", text);
    
    NSArray *arr = [text componentsSeparatedByString:@"\n"];
    Person*item;
    NSMutableArray *tmp = [NSMutableArray new];
    for(NSString*p in arr){
        if([p hasPrefix:@"BEGIN"])
        {
            item=[Person new];
        }else if([p hasPrefix:@"END"])
        {
            [tmp addObject:item];
        }else if([p hasPrefix:@"TEL"])
        {
            
            NSArray *components=[p componentsSeparatedByString:@":"];
            NSString*app=components[1];
            item.haoma=app;
        }
        else if ([p hasPrefix:@"FN;"]){
            NSArray *upperComponents = [p componentsSeparatedByString:@":"];
            NSString *name = upperComponents[1];
            name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            name = [name stringByRemovingPercentEncoding];
            
            // NSlog(@"FN:%@",name);
            item.name = name;
        }
            

        aabb=tmp;
    }

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //return data.count;
    return aabb.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 //   UITableViewCell*call=[[UITableViewCell alloc]initWithStyle: UITableViewCellStyleValue1 reuseIdentifier:nil];
 //   call.textLabel.text=data[indexPath.row];
   
    
    /*UITableViewCell*call=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1
        reuseIdentifier:nil];
    Person*p= data[indexPath.row];
    call.textLabel.text=p.bianhao;
    call.detailTextLabel.text=p.name;
    return call;*/
    UITableViewCell * cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    Person * p = aabb[indexPath.row];
    cell.textLabel.text = p.name;
    cell.detailTextLabel.text = p.haoma;
    return cell;

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
